﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Services
{
    public class DB_User
    {
        public bool InsertUser(User user)
        {
            if (!CheckUserExist(user.Uid.ToString()))
            {
                return false;
            }
            string sql = "insert into [user] values(@Name,@Pwd,@Birthday,@Hobbies,@uid,@QQ,@Email,@Phone)";
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@Name",user.Name),
                new SqlParameter("@Pwd",user.Pwd),
                new SqlParameter("@Birthday",user.Hobbies),
                new SqlParameter("@Hobbies",user.Hobbies),
                new SqlParameter("@uid",user.Uid),
                new SqlParameter("@QQ",user.QQ),
                new SqlParameter("@Email",user.Email),
                new SqlParameter("@Phone",user.Phone)
            };
            int count = 0;
            try
            {
                count = SQLHELP.GetNonQueryWithParm(sql, parm);
            }
            catch (Exception )
            {
                //异常日志
                throw;
            }
            if (count == 1)
            {
                return true;
            }
            return false;
        }

        public bool UpdateUser(User user)
        {
            string sql = "update [user] set name=@Name,Pwd=@Pwd,Birthday=@Birthday,Hobbies=@Hobbies,QQ=@QQ,Email=@Email,Phone=@Phone where uid=@uid";
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@Name",user.Name),
                new SqlParameter("@Pwd",user.Pwd),
                new SqlParameter("@Birthday",user.Birthday),
                new SqlParameter("@Hobbies",user.Hobbies),
                new SqlParameter("@QQ",user.QQ),
                new SqlParameter("@Email",user.Email),
                new SqlParameter("@Phone",user.Phone),
                new SqlParameter("@uid",user.Uid)
            };
            int count = 0;
            try
            {
                count = SQLHELP.GetNonQueryWithParm(sql, parm);
            }
            catch (Exception)
            {
                //异常日志
                throw;
            }
            if (count == 1)
            {
                return true;
            }
            return false;
        }

        public bool DeleteUser(string id)
        {
            string sql = "delete [user] where uid=@uId";
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@uId",id)
            };
            int count = 0;
            try
            {
                count = SQLHELP.GetNonQueryWithParm(sql, parm);
            }
            catch (Exception)
            {
                //异常日志
                throw;
            }
            if (count == 1)
            {
                return true;
            }
            return false;
        }

        public User GetUserById(string uid)
        {
            string sql = "select * from [user] where uid =@uid";

            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@uid",uid)
            };

            DataTable dt = new DataTable();

            try
            {
                dt = SQLHELP.GetDataTableWithParm(sql, parm);
            }
            catch (Exception)
            {
                throw;
            }
            if (dt == null || dt.Rows.Count == 0)
            {
                return null;
            }
            else {
                User user = new User();
                user.Uid = uid;
                user.Name = dt.Rows[0]["Name"].ToString();
                user.Pwd = dt.Rows[0]["Pwd"].ToString();
                user.Birthday = dt.Rows[0]["Birthday"].ToString();
                user.Hobbies = dt.Rows[0]["Hobbies"].ToString();
                user.QQ = dt.Rows[0]["QQ"].ToString();
                user.Email = dt.Rows[0]["Email"].ToString();
                user.Phone = dt.Rows[0]["Phone"].ToString();
                return user;
            }
        }

        public bool CheckUserExist(string uid)
        {
            User user = GetUserById(uid);
            if (user == null)
            {
                return true;
            }
            return false;
        }

        public List<User> GetAllUser()
        {
            List<User> list = new List<User>();
            String sql = "select * from [user]";

            DataTable dt = new DataTable();

            try
            {
                dt = SQLHELP.GetDataTableNoParm(sql);
            }
            catch (Exception)
            {
                throw;
            }
            if (dt == null || dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                return ConvertListAndTable.DataTableToList<User>(dt);
            }
        }
    }
}
